package spider;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Spider {

	public static void main(String[] args) {
		String URL = "https://www.bilibili.com/";
		spiderDemo(URL);
	}

	private static void spiderDemo(String url) {
		Connection connection = Jsoup.connect(url);
		try {
			Document doc = connection.timeout(10000).get();
			Elements elements = doc.getElementsByTag("img");
			for (int i = 0; i < elements.size(); i++) {
				Element element = elements.get(i).getElementsByTag("img").first();
				String src = element.attr("abs:src");
				System.out.println(src);
				download(src);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void download(String src) throws IOException {
		URL url = new URL(src);
		InputStream is = url.openStream();
		File dir = new File("F:"+File.separator+"downpics");
		if (!dir.exists()) {
			dir.mkdirs();
		}
		File file = new File(dir,System.currentTimeMillis()+".jpg");
		FileOutputStream fos = new FileOutputStream(file);
		byte[] buffer = new byte[1024];
		int len;
		while((len=is.read(buffer))!=-1) {
			fos.write(buffer, 0, len);
		}
		is.close();
		fos.flush();
		fos.close();
	}
}
